#include "myhead.h"
//客户端
int kfbsock;
char path[50] = "";
pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
void *copy_file(void *arg)
{
    pthread_mutex_lock(&mymutex);
    sleep(1);
    char if_have[50]="";
    int ret=recv(kfbsock,if_have,sizeof(if_have),0);
    if( ret==-1)
    {
        perror("recv");
        return NULL;
    }
    else if(ret == 0)
    {
        exit(-1);
    }
    if(strcmp(if_have,"have")==0)
    {
        char mypath[50] = "";
        snprintf(mypath,sizeof(mypath),"/%s",basename(path));
        printf("%s\n",mypath);
        if(access(mypath,F_OK)== 0)
        {
            printf("该路径已经存在\n");
            printf("-----输入1强行替换,2退出------\n");
            int temp=0;
            scanf("%d",&temp);
            if(temp == 1)
            {
                int fd = open(mypath,O_CREAT|O_TRUNC|O_WRONLY,0777);//注意掩码
                if(fd == -1)
                {
                    perror("open");
                    return NULL;
                }
                char copy[512]="";
                while(1)
                {
                    
                    int ret = recv(kfbsock,copy,sizeof(copy),0);
                    if(ret ==-1)
                    {
                        perror("recv");
                        return NULL;
                    }
                    else if(ret == 0)
                    {
                        exit(-1);
                    }
                    if(ret >= 6 && memcmp(copy + ret - 6, "all_ok", 6) == 0)
                    {
                        write(fd, copy, ret - 6);    // 只写文件部分
                        printf("传输完毕\n");
                        pthread_mutex_unlock(&mymutex);
                        usleep(2000);
                        break;
                    }
                    write(fd,copy,ret);
                    bzero(copy,sizeof(copy));
                }
                close(fd);
            }
            else 
            {
                pthread_mutex_unlock(&mymutex);
                usleep(2000);
            }
        }
        else
        {
            int fd = open(mypath,O_CREAT|O_WRONLY|O_EXCL,0777);
            if(fd == -1)
            {
                perror("open");
                return NULL;
            }
            char copy[512]="";
            while(1)
            {
                int ret = recv(kfbsock,copy,sizeof(copy),0);
                if(ret ==-1)
                {
                    perror("recv");
                    return NULL;
                }
                else if(ret == 0)
                {
                    exit(-1);
                }
                if(strcmp(copy,"all_ok")==0)
                {
                    printf("传输完毕\n");
                    break;
                }
                write(fd,copy,sizeof(copy));
                bzero(copy,sizeof(copy));
            }
            close(fd);
        }
    }
    else
    {
        printf("对面没有这个路径\n");
        pthread_mutex_unlock(&mymutex);
        usleep(2000);
    }
}

int main()
{
    kfbsock = socket(AF_INET, SOCK_STREAM, 0);
    if(kfbsock == -1)
    {
        perror("socket");
        return -1;
    }

    int opt = 1;
    setsockopt(kfbsock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

    struct sockaddr_in myaddr = 
    {
        .sin_family = AF_INET,
        .sin_port = htons(10000),
        .sin_addr.s_addr = INADDR_ANY
    };
    
    if(bind(kfbsock, (struct sockaddr *)&myaddr, sizeof(myaddr)) == -1)
    {
        perror("bind");
        close(kfbsock);
        return -1;
    }

    struct sockaddr_in conaddr = 
    {
        .sin_family = AF_INET,
        .sin_port = htons(5000)
    };
    
    if(inet_pton(AF_INET, "192.168.72.52", &conaddr.sin_addr) != 1)
    {
        perror("inet_pton");
        close(kfbsock);
        return -1;
    }

    if(connect(kfbsock, (struct sockaddr *)&conaddr, sizeof(conaddr)) == -1)
    {
        perror("connect");
        close(kfbsock);
        return -1;
    }

    printf("连接到服务器成功\n");

    char buf[50]="";
    while (1)
    {
        pthread_mutex_lock(&mymutex);
        bzero(buf,sizeof(buf));
        printf("请输入指令\n");
        scanf("%s",buf);
        if(!strcmp(buf,"get"))
        {
            printf("请输入路径名\n");
            bzero(path,sizeof(path));
            scanf("%s",path);
            if(send(kfbsock,path,strlen(path),0)==-1)
            {
                perror("发送路径名错误");
                return -1;
            }
        }
        pthread_mutex_unlock(&mymutex);
        pthread_t copy_tid;
        if(pthread_create(&copy_tid,NULL,copy_file,NULL)==-1)
        {
            perror("pthread_create");
            return -1;
        }
        usleep(2000);
    }
    close(kfbsock);
}